{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "from pytorch_metric_learning.distances import LpDistance, CosineSimilarity, DotProductSimilarity\n",
    "from pytorch_metric_learning.losses import NTXentLoss, SupConLoss\n",
    "from pytorch_metric_learning.reducers import AvgNonZeroReducer, PerAnchorReducer\n",
    "from pytorch_metric_learning.utils import common_functions as c_f\n",
    "\n",
    "import os\n",
    "\n",
    "TEST_DEVICE = torch.device(os.environ.get(\"TEST_DEVICE\", \"cuda\"))\n",
    "\n",
    "dtypes_from_environ = os.environ.get(\"TEST_DTYPES\", \"float16,float32,float64\").split(\n",
    "    \",\"\n",
    ")\n",
    "TEST_DTYPES = [getattr(torch, x) for x in dtypes_from_environ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.1889, -2.0954, -2.1622, -0.9798, -0.2999,  0.2575, -0.5817, -1.2394,\n",
      "          0.0636,  0.6927,  1.4599, -0.7563, -0.7357],\n",
      "        [ 0.1996,  0.8045, -0.1687,  0.2859,  0.8299,  0.3390, -1.0150,  0.0772,\n",
      "          1.9016, -0.7265, -0.2821,  1.4018,  1.9424],\n",
      "        [ 1.3193, -1.2767,  0.8210, -1.9712, -0.9279, -1.7080,  0.6796, -0.8085,\n",
      "         -0.8814, -1.5720, -1.0460,  1.9520, -0.3893],\n",
      "        [ 1.5262, -1.4883, -0.8948,  0.3876, -0.0660,  0.0590,  0.8836, -0.2283,\n",
      "          0.8427, -0.8171, -0.3715,  0.6567,  0.8577],\n",
      "        [ 1.0530, -0.5048, -0.0338, -0.0904,  0.1101, -0.3497, -1.4319,  0.1921,\n",
      "          0.2075, -1.4596, -0.6132, -0.4351,  1.1964],\n",
      "        [-0.6290, -0.3426, -1.1992,  0.4503, -0.7431,  0.1220, -1.9100,  1.2934,\n",
      "          2.0955,  0.1623,  1.5234,  1.1079, -0.2712]])\n"
     ]
    }
   ],
   "source": [
    "# embeddings\n",
    "batch_size = 6\n",
    "hidden_dim = 13\n",
    "\n",
    "embeddings = torch.randn((batch_size, hidden_dim))\n",
    "print(embeddings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# labels\n",
    "labels = torch.LongTensor([0, 0, 0, 1, 1, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SupConLoss(\n",
      "  (distance): CosineSimilarity()\n",
      "  (reducer): AvgNonZeroReducer()\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "# loss_funcA\n",
    "temperature = 0.1\n",
    "\n",
    "loss_funcA = SupConLoss(temperature=temperature)\n",
    "print(loss_funcA)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(3.7643) <class 'torch.Tensor'>\n"
     ]
    }
   ],
   "source": [
    "# 计算 loss A\n",
    "loss_A = loss_funcA(embeddings, labels).float()\n",
    "print(loss_A, type(loss_A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.0409) tensor(25.4132) tensor(6.4322)\n",
      "tensor(0.8321) tensor(25.4132) tensor(3.4190)\n",
      "tensor(0.0409) tensor(185.0638) tensor(8.4176)\n",
      "tensor(0.3266) tensor(185.0638) tensor(6.3398)\n",
      "tensor(0.8321) tensor(50.0056) tensor(4.0959)\n",
      "tensor(0.3266) tensor(50.0056) tensor(5.0312)\n",
      "tensor(54.9212) tensor(125.7912) tensor(0.8287)\n",
      "tensor(54.9212) tensor(169.1595) tensor(1.1249)\n",
      "total_lossA:  tensor(3.7643, dtype=torch.float64) <class 'torch.Tensor'>\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "Float did not match Double",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-076b5a3d09ce>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     47\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     48\u001b[0m \u001b[0mrtol\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1e-4\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 49\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mloss_A\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtotal_lossA\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrtol\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mrtol\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m: Float did not match Double"
     ]
    }
   ],
   "source": [
    "# 复现计算过程\n",
    "# 注意：distance默认是 CosineSimilarity()\n",
    "\n",
    "pos_pairs = [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1), (3, 4), (4, 3)]\n",
    "neg_pairs = [\n",
    "    (0, 3), (0, 4), (0, 5),\n",
    "    (1, 3), (1, 4), (1, 5),\n",
    "    (2, 3), (2, 4), (2, 5),\n",
    "    (3, 0), (3, 1), (3, 2), (3, 5),\n",
    "    (4, 0), (4, 1), (4, 2), (4, 5),\n",
    "    (5, 0), (5, 1), (5, 2), (5, 3), (5, 4),\n",
    "]\n",
    "\n",
    "total_lossA = torch.zeros(5, device=\"cpu\", dtype=torch.float64)\n",
    "\n",
    "for a1, p in pos_pairs:\n",
    "    # a1: anchor;\n",
    "    # p: positive instance\n",
    "    \n",
    "    anchor, positive = embeddings[a1], embeddings[p]\n",
    "    numeratorA = torch.exp(torch.matmul(anchor, positive) / (temperature * torch.norm(anchor) * torch.norm(positive)))\n",
    "    \n",
    "    denominatorA = 0\n",
    "    \n",
    "    for a2, n in pos_pairs + neg_pairs:\n",
    "        # n: negative instance\n",
    "        \n",
    "        if a2 == a1:\n",
    "            negative = embeddings[n]\n",
    "            curr_denomD = torch.exp(\n",
    "                torch.matmul(anchor, negative) / (temperature * torch.norm(anchor) * torch.norm(negative))\n",
    "            )\n",
    "            denominatorA += curr_denomD\n",
    "            \n",
    "        else:\n",
    "            continue\n",
    "    \n",
    "    print(numeratorA, denominatorA, - torch.log(numeratorA / denominatorA))\n",
    "    curr_lossA = -torch.log(numeratorA / denominatorA)\n",
    "    total_lossA[a1] += curr_lossA\n",
    "\n",
    "pos_pair_per_anchor = torch.tensor(\n",
    "    [2, 2, 2, 1, 1], device=\"cpu\", dtype=torch.float64\n",
    ")\n",
    "total_lossA = torch.mean(total_lossA / pos_pair_per_anchor)\n",
    "print(\"total_lossA: \", total_lossA, type(total_lossA))\n",
    "\n",
    "rtol = 1e-4\n",
    "assert torch.isclose(loss_A, total_lossA, rtol=rtol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
